/**
 * 3η ΓΕ 2013-2014 - Super Market
 * ΦΡΑΓΚΟΥΛΟΠΟΥΛΟΥ ΕΜΜΑΝΟΥΕΛΑ (69298)
 * ΧΑΛΔΑΣ ΧΑΡΑΛΑΜΠΟΣ (69305)
 * ΧΟΡΜΟΒΙΤΗΣ ΙΩΑΝΝΗΣ (69325)
 * ΧΟΥΣΤΟΥΛΑΚΗ ΕΥΑΓΓΕΛΙΑ (69327)
 */
package customerGUI;

import java.awt.Component;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.TypedQuery;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.INFORMATION_MESSAGE;
import static javax.swing.JOptionPane.WARNING_MESSAGE;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import model.*;
import supermarket.EntityManagerFactory;


/**
 * Οθόνη: Καλάθι Αγορών
 */
public class BasketDialog extends javax.swing.JDialog {
    private Customer myCustomer;
    private int tries = 3;
    int totalPoints = 0;    // Συνολικοί Πόντοι
    float totalAmount = 0;  // Συνολική Αξία
    int chequesTotal = 0;   // Συνολική Αξία Επιταγών
	
    
    /**
     * Creates new form BasketDialog
     */
    public BasketDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        NumberFormat nf = new DecimalFormat("#,##0.00");
        TableCellRenderer dtcr = new DefaultTableCellRenderer() {
            

            @Override
            public Component getTableCellRendererComponent(JTable table,
                    Object value, boolean isSelected, boolean hasFocus,
                    int row, int column) {
                NumberFormat nf = new DecimalFormat("#,##0.00");
                if (value instanceof Float) {
                    value = nf.format(value);
                    setHorizontalAlignment(SwingConstants.RIGHT);
                } else if (value instanceof Integer) {
                    setHorizontalAlignment(SwingConstants.RIGHT);
                } else {
                    setHorizontalAlignment(SwingConstants.LEFT);
                }
                return super.getTableCellRendererComponent(table, value, isSelected,
                        hasFocus, row, column);
            }
        };
        
        for (int i=0;i<basketTable.getModel().getColumnCount();++i) {
            basketTable.getColumnModel().getColumn(i).setCellRenderer(dtcr);
        }
        
        
        DefaultTableModel model = (DefaultTableModel) basketTable.getModel();
        Basket b = Basket.getInstance();
        List<ProductPurchase> pp = b.getList();
        
        int points = 0; // Πόντοι Αναλυτικής Γραμμής
        float sum = 0;  // Αξία Αναλυτικής Γραμμής
        
        totalPoints = 0;    // Συνολικοί Πόντοι
        totalAmount = 0;  // Συνολική Αξία
        
        for (int i=0;i<pp.size();++i) {
            // Ποσότητα
            int quantity = pp.get(i).getQuantity();
            // Πόντοι (Ποσότητα * Πόντοι Προϊόντος)
            points = quantity * pp.get(i).getProductId().getPoints();
            totalPoints += points;
            // Αξία (Ποσότητα * Τιμή Μονάδας)
            sum = quantity * pp.get(i).getProductId().getPrice();
            totalAmount += sum;
            // Add Row
            model.addRow(new Object[]{pp.get(i).getProductId().getName(),
                quantity,
                pp.get(i).getProductId().getPoints(),
                points,
                pp.get(i).getProductId().getPrice(),
                sum});	
   
        }
        pointsText.setText(totalPoints + "");
        totalText.setText(nf.format(totalAmount) + " €");
        chequesAmount.setText(nf.format(0) + " €");
        amountToPay.setText(nf.format(totalAmount) + " €");
    }

    public void setCustomer(Customer c) {
        myCustomer = c;

        SimpleDateFormat f = new SimpleDateFormat("dd/MM/yy");
        
        TypedQuery<Cheques> q = EntityManagerFactory.getEntityManager()
                    .createQuery("SELECT c from Cheques c " +
                            "where c.customerId = :customerId and " + 
                            "c.isvalid = :isvalid", Cheques.class)
                    .setParameter("customerId", myCustomer)
                    .setParameter("isvalid", true);
        List<Cheques> ls = q.getResultList();
        
        
        DefaultTableModel model = (DefaultTableModel) chequesTable.getModel();
        
        for (int i=0;i<ls.size();++i) {	
            model.addRow(new Object[]{false,
                ls.get(i).getChequeId(), 
                f.format(ls.get(i).getCreationDate())
            });
        }
        
        // Radio Button Τρόπου Παραλαβής
        if (c.getAddress().isEmpty()) {
            this.sendInPlaceRadio.setEnabled(false);
            this.getFromStoreRadio.setSelected(true);
        }
        else {
            this.sendInPlaceRadio.setSelected(false);
            this.getFromStoreRadio.setSelected(true);
        }
    }
	
    /**
     * This method is called from within the constructor to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        basketTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        getFromStoreRadio = new javax.swing.JRadioButton();
        makePurchaseButton = new javax.swing.JButton();
        sendInPlaceRadio = new javax.swing.JRadioButton();
        cancelButton = new javax.swing.JButton();
        pointsLabel2 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        chequesTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        pointsText = new javax.swing.JTextField();
        totalText = new javax.swing.JTextField();
        pointsLabel = new javax.swing.JLabel();
        chequesAmount = new javax.swing.JTextField();
        amountToPay = new javax.swing.JTextField();
        totalLabel = new javax.swing.JLabel();
        chequesAmountLabel = new javax.swing.JLabel();
        amountToPayLabel = new javax.swing.JLabel();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Καλάθι Αγορών");

        basketTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Προϊόν", "Ποσότητα", "Πόντοι Είδους", "Πόντοι Αγοράς", "Τιμή μονάδας", "Σύνολική Αξία"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Float.class, java.lang.Float.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(basketTable);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Επιλεγμένα προϊόντα");

        getFromStoreRadio.setText("Παραλαβή από το κατάστημα");
        getFromStoreRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getFromStoreRadioActionPerformed(evt);
            }
        });

        makePurchaseButton.setText("Ολοκλήρωση αγορών");
        makePurchaseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                makePurchaseButtonActionPerformed(evt);
            }
        });

        sendInPlaceRadio.setText(" Αποστολή στο χώρο");
        sendInPlaceRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendInPlaceRadioActionPerformed(evt);
            }
        });

        cancelButton.setText("Ακύρωση");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        pointsLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        pointsLabel2.setText("Επιταγές");

        chequesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Επιλογή", "Αριθμός Επιταγής", "Ημ/νία Επιταγής"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        chequesTable.setColumnSelectionAllowed(true);
        chequesTable.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                chequesTablePropertyChange(evt);
            }
        });
        jScrollPane4.setViewportView(chequesTable);
        chequesTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        pointsText.setEditable(false);
        pointsText.setBackground(new java.awt.Color(255, 255, 204));
        pointsText.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        totalText.setEditable(false);
        totalText.setBackground(new java.awt.Color(255, 255, 204));
        totalText.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        pointsLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        pointsLabel.setText("Πόντοι Αγοράς :");

        chequesAmount.setEditable(false);
        chequesAmount.setBackground(new java.awt.Color(255, 255, 204));
        chequesAmount.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        amountToPay.setEditable(false);
        amountToPay.setBackground(new java.awt.Color(255, 255, 204));
        amountToPay.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        totalLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        totalLabel.setText("Συνολική Αξία :");

        chequesAmountLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        chequesAmountLabel.setText("Αξία Επιταγών :");

        amountToPayLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        amountToPayLabel.setText("Πληρωτέο Ποσό :");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(32, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, amountToPayLabel)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, chequesAmountLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, amountToPay, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, chequesAmount)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .add(pointsLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(pointsText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .add(totalLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(totalText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(new java.awt.Component[] {amountToPay, chequesAmount, pointsText, totalText}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(pointsText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(pointsLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(totalText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(totalLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(chequesAmount, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(chequesAmountLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(amountToPay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(amountToPayLabel))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 156, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(pointsLabel2)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane1)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(sendInPlaceRadio)
                                        .add(27, 27, 27)
                                        .add(getFromStoreRadio))
                                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 406, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 45, Short.MAX_VALUE)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(layout.createSequentialGroup()
                                        .add(makePurchaseButton)
                                        .add(18, 18, 18)
                                        .add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 148, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(pointsLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 172, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(getFromStoreRadio)
                            .add(sendInPlaceRadio)))
                    .add(layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(makePurchaseButton)
                            .add(cancelButton))))
                .add(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

     private void makePurchaseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_makePurchaseButtonActionPerformed
        Basket b = Basket.getInstance();
        if (b.size() == 0) {
            return;
        }
        
        if (totalAmount - chequesTotal < 0) {
            JOptionPane.showMessageDialog(this, "Το Πληρωτέο Ποσό δεν πρέπει " +
                    "να είναι αρνητικό.",
                     "Προσοχή!", WARNING_MESSAGE);
            return;
        }
        
        MakePurchaseDialog pd = new MakePurchaseDialog(null, true);
        pd.setCustomer(myCustomer);
        pd.setLocationRelativeTo(null);
        pd.setVisible(true);
        if (pd.isVerified()) {
            // Αποστολή στην Ταχυδρομική Διεύθυνση;
            boolean sendAtHome = this.sendInPlaceRadio.isSelected();

            List<Cheques> chequesList = myCustomer.getChequesList();
            List<Cheques> selectedCheques = new ArrayList<>();
 
            for (int i=0; i<chequesTable.getRowCount(); i++) {
                Object oSelected = chequesTable.getValueAt(i, 0);
                boolean isSelected = Boolean.valueOf(oSelected.toString());
            
                if (isSelected) {
                    int chequeID = (int) chequesTable.getValueAt(i, 1);
                    
                    for (Cheques cheque: chequesList) {
                        if (cheque.getChequeId() == chequeID) {
                            selectedCheques.add(cheque);
                        }
                    }
                }
           }
        
     

            b.goPay(myCustomer, selectedCheques, sendAtHome);
            
            JOptionPane.showMessageDialog(this, "H Συναλλαγή ολοκληρώθηκε "+
                    "με επιτυχία.",
                     "Ολοκλήρωση Συναλλαγής", INFORMATION_MESSAGE);
            b.clear();
            dispose();
        } 
        else {
            --tries;
            if (tries <= 0) {
                JOptionPane.showMessageDialog(this, "Πραγματοποιήσατε 3 " +
                        "αποτυχημένες προσπάθειες επικοινωνίας\n" +
                        "με την Αρχή Πιστοποίησης Πιστωτικών Καρτών.\n" +
                        "Η Αγορά ακυρώθηκε.",
                        "Ακύρωση αγοράς", WARNING_MESSAGE);
                b.clear();
                dispose();
            }
        }
     }//GEN-LAST:event_makePurchaseButtonActionPerformed

     private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        Basket b = Basket.getInstance();
        if (b.size() == 0) {
                dispose();
                return;
        }
        int dialogButton = JOptionPane.YES_NO_OPTION;
        int rc = JOptionPane.showConfirmDialog(this, "Καθαρισμός καλαθιού", 
                   "Προσοχή", 
                   dialogButton);
        if (rc == JOptionPane.YES_OPTION) {
                b.clear();
        }
        dispose();
     }//GEN-LAST:event_cancelButtonActionPerformed

     private void sendInPlaceRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendInPlaceRadioActionPerformed
          // TODO add your handling code here:
        this.getFromStoreRadio.setSelected(false);
        this.sendInPlaceRadio.setSelected(true);
     }//GEN-LAST:event_sendInPlaceRadioActionPerformed

     private void getFromStoreRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getFromStoreRadioActionPerformed
     // TODO add your handling code here:
          this.getFromStoreRadio.setSelected(true);
          this.sendInPlaceRadio.setSelected(false);
     }//GEN-LAST:event_getFromStoreRadioActionPerformed

    private void chequesTablePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_chequesTablePropertyChange
        
        if (evt.getPropertyName().equals("tableCellEditor")) {
            calcSelectedCheques();
        }
    }//GEN-LAST:event_chequesTablePropertyChange

    private void calcSelectedCheques () {
        int countOfSelectedCheques = 0;
        for (int i=0; i<chequesTable.getRowCount(); i++) {
            Object oSelected = chequesTable.getValueAt(i, 0);
            boolean isSelected = Boolean.valueOf(oSelected.toString());
            
            if (isSelected) {
                countOfSelectedCheques++;
            }
            chequesTotal = 6 * countOfSelectedCheques;
            NumberFormat nf = new DecimalFormat("#,##0.00");
            chequesAmount.setText(nf.format(chequesTotal) + " €");
            
            float toPay = totalAmount - chequesTotal;
            amountToPay.setText(nf.format(toPay) + " €");
            
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField amountToPay;
    private javax.swing.JLabel amountToPayLabel;
    private javax.swing.JTable basketTable;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField chequesAmount;
    private javax.swing.JLabel chequesAmountLabel;
    private javax.swing.JTable chequesTable;
    private javax.swing.JRadioButton getFromStoreRadio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton makePurchaseButton;
    private javax.swing.JLabel pointsLabel;
    private javax.swing.JLabel pointsLabel2;
    private javax.swing.JTextField pointsText;
    private javax.swing.JRadioButton sendInPlaceRadio;
    private javax.swing.JLabel totalLabel;
    private javax.swing.JTextField totalText;
    // End of variables declaration//GEN-END:variables
}
